<?php

    header("Content-type: text/html; charset=utf-8"); 
	class db{

		private $_conn;

		public function __construct(){
            $this->initDB();
        }

        private function initDB(){
        	require_once("config.db.php");
        	$this->connect($db_config);
        } 

        private function connect($config){
        	$this->_conn = @mysql_connect($config['hostname'], $config['username'], $config['password']);
        	if(!$this->_conn){
        		echo 'mysql connect error';
        		exit();
        	}
        	if(!@mysql_select_db($config['dbname'], $this->_conn)){
        		echo 'mysql select error';
        		exit();
        	}
        	@mysql_query("set names " . $config['chartset']);
        }

        public function query($sql){
        	$query = mysql_query($sql, $this->_conn);
        	if(!$query){
        		echo 'query error';
        		exit();
        	}
        	return $query;
        }

        public function getRecord($sql){
        	$query = $this->query($sql);
        	return mysql_fetch_array($query);
        }

        public function addRecord($table ,$data){
        	$fields = '';
        	$values = '';
        	if(empty($data)){
        		return false;
        	}
        	while (list($key, $value) = each($data)) {
        		$fields .= $key . ',';
        		$values .= $value . ',';
        	}
        	$fields = substr($fields, 0, -1);
        	$values = substr($values, 0, -1);
        	$sql 	= 'insert into ' . $table . '(' . $fields . ')value(' . $values . ')';
        	if(!$this->query($sql)){
        		return false;
        	}
        	return true;
        }
	}
?>